//
//  GameScene.cpp
//  Frog Jump
//
//  Created by acumen on 7/26/13.
//
//

#include "GameScene.h"


bool GameScene::init() {
    if (!CCLayer::init()) {
        return false;
    }
    CCSize winSize = GameController::getWinSize();
    
    CCSpriteFrameCache *pFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
    pFrameCache->addSpriteFramesWithFile(SPRITE_PLIST, SPRITE_PNG);

    
    pArrFrames = CCArray::create();
    char strFrameName[100] = {0};
    for (int i = 1; i <= 7; i++) {
        sprintf(strFrameName, "frog%d.png",i);
        CCSpriteFrame *pFrame = pFrameCache->spriteFrameByName(strFrameName);
        pArrFrames->addObject(pFrame);
    }
    
    CCAnimation *pAnimation = CCAnimation::createWithSpriteFrames(pArrFrames);
    
    pFrog = CCSprite::createWithSpriteFrameName("frog1.png");
    pFrog->setPosition(CCPointMake(winSize.width/2, winSize.height/2));
    
    pAction = CCRepeatForever::create(CCAnimate::create(pAnimation));
    pAction->retain();
    addChild(pFrog);
    pFrog->runAction(pAction);
    pFrog->retain();
    // Btn back
    CCMenuItemLabel *pBtnBack = CCMenuItemLabel::create(CCLabelTTF::create(STR_BACK, FONT_MENU, 56), this, menu_selector(GameScene::backToMenuScene));
    // Menu
    CCMenu *pMenuBack = CCMenu::create(pBtnBack, NULL);
    pMenuBack->setAnchorPoint(CCPointZero);
    pMenuBack->setPosition(CCPointMake(100, 64));
    setTouchEnabled(true);
    addChild(pMenuBack);
    
    scheduleUpdate();
    return true;
}

CCScene* GameScene::scene() {
    CCScene *pScene = CCScene::create();
    GameScene *pLayer = GameScene::create();
    pScene->addChild(pLayer);
    return pScene;
}

GameScene::~GameScene() {
    if (pAction != NULL) {
        pAction->release();
        pAction = NULL;
    }
    
    if (pFrog != NULL) {
        pFrog->release();
        pFrog = NULL;
    }
}

void GameScene::update(float dt) {
    
}

void GameScene::registerWithTouchDispatcher() {
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}

bool GameScene::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {
    
    return true;
}

void GameScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {
    
}

